package com.cw.wh.controller;


import com.cw.wh.base.R;
import com.cw.wh.base.Rcode;
import com.cw.wh.controller.po.UserPo;
import com.cw.wh.controller.vo.UserVo;
import com.cw.wh.dao.entity.TxUser;
import com.cw.wh.service.TxUserService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.lang.NonNullApi;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Component
@RestController
@Api(value = "测试用户相关接口")
public class UserController {

    @Resource
    private TxUserService txUserService;

    /**
     * 查询所有数据
     * @return
     */
    @ApiOperation(value = "查询用户列表",notes = "查询所有的用户数据" ,httpMethod = "GET")
    @ApiResponses({
            @ApiResponse(code = 0 , message = "成功")
    })
    @RequestMapping("/findAll")
    public R findAll(){
        return R.ok(txUserService.list());
    }

    /**
     * 查询封装后的数据
     * @return
     */
    @ApiOperation(value = "查询用户手机列表",notes = "查询用户组装后数据",httpMethod = "GET")
    @RequestMapping("/findUserAndPhone")
    public R findUserAndPhone(){
        return R.ok(txUserService.list().stream().map(m ->{ return new UserVo(m.getName() + " : " + m.getPhone());}).collect(Collectors.toList()));
    }

    /**
     *
     * @param userPo
     * @return
     */
    @ApiOperation(value = "新增用户",notes = "新增用户接口",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户ID" , paramType = "query" , name = "id" , required = true , dataType = "Integer"),
            @ApiImplicitParam(value = "用户名称" , paramType = "query" , name = "name" , required = true , dataType = "String"),
            @ApiImplicitParam(value = "用户手机号" , paramType = "query" , name = "phone" , required = true , dataType = "String")

    })
    @PostMapping("/saveUser")
    public R saveUser(@RequestBody UserPo userPo){
        if(null == userPo.getId()){
            return R.error(Rcode.PARAM_ERR);
        }
        //加上其他的参数检验
        TxUser user = new TxUser();
        BeanUtils.copyProperties(userPo , user);
        boolean b = txUserService.save(user);
        if(!b){
            return R.error(Rcode.SQL_ERR);
        }
        return R.ok();
    }

    @RequestMapping("/oll")
    @ApiIgnore
    public R oll(@RequestBody UserPo userPo){
        System.out.println("aaa");
        return R.ok(userPo);
    }
}
